
var  a=$(window).width()
$("body").css('width',a+'px');

//初始化广告图
advertisementList();
//分类
sortTypeMethod();
//店铺分类
sortTypeMethod1();
var styleSort = "";
//找货
selectGoodsByZhaohuo(styleSort);

var sortTypeId = "";
//求购
selectGoodsByQiugou(sortTypeId);
//尾货
selectGoodsByWeiHuo(styleSort);
//商圈
selectAreaList();
//店铺
selectShopList(sortTypeId);
//金币中心
selectGoodsByGoldRate(styleSort);
var goodsType = 1; //商品类型
var lookTime = ''; //关注度
var priceLevel = ''; //商品档次

//按条件查询商品
selectGoodsByGoodsType(goodsType, lookTime, priceLevel);

//获取当前用户的信息
//if(userId!=''){
//getUserInfoByUserId()
//}

var currentPage = 1; //分页
//找货
function selectGoodsByZhaohuo(styleSort) {
	$("#selectGoodsList").html("");
	$.ajax({
		url: urlcore + "/api/goods/selectGoodsList?propertyTypeId=" + styleSort + "&size=" + 8 + "&current=" + 1,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				var  listArrays=data.data.list;
				console.log(data.data.list[0].coverUrl)
				$.each(data.data.list, function(i, n) {
					
					var coverUrl=n.coverUrl==null?'http://app.jiagangwangluo.com:8080/api//ATTACHMENT/cab06117-6607-401a-843d-5ac103323fb6/default.png':n.coverUrl;
					coverUrl=isHasImg(coverUrl); 					var goodsTitle=n.goodsTitle==null?'暂无':n.goodsTitle;
 					var tradePrice=n.tradePrice==null?'0':n.tradePrice;
 					var collectNum=n.collectNum==null?'0':n.collectNum;
 					var thislist =
 						'<dl>' +
						'<a href="2.2.0goods_detail.html?id=' + n.id + '">' +
						'<dt>' +
						'<img class="zhaohuoclass" src="' + coverUrl + '"  style="width: 229px;height: 290px;"/>' +
						'</dt>' +
						'<dd>' +
						'<h5>' + goodsTitle + '</h5>' +
 						'<h6>&yen;' + tradePrice + '<span>收藏' + collectNum + '</span></h6>' +
 						'</dd>' +
 						'</a>' +
 						'</dl>';
 					$('#selectGoodsList').append(thislist);
 				});
			}
			
			//固定宽高
			$(function(){
				var imgList1 = document.getElementsByClassName("zhaohuoclass");  
			    var w=290;
			    var h=290;
			    for(var i=0;i<imgList1.length;i++){
			        if(imgList1[i].width>w || imgList1[i].height>h){  
			            imgList1[i].width = w;  
			            imgList1[i].heigth = h;  
			        }  
			    }  
			})
		},
		error: function() {
			layer.alert("error");
		}
	});
}

function selectGoodsByStyleSort(styleSort) {
	selectGoodsByZhaohuo(styleSort);
}
//求购
function selectGoodsByQiugou(sortTypeId) {
	$("#QiuGouGoodsList").html("");
	$.ajax({
		url: urlcore + "/api/wantBuy/selectList?sortTypeId=" + sortTypeId + "&size=" + 15 + "&current=" + 1,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				$.each(data.data.list, function(i, n) {
					var coverUrl=n.coverUrl==null?'http://app.jiagangwangluo.com:8080/api//ATTACHMENT/cab06117-6607-401a-843d-5ac103323fb6/default.png':n.coverUrl;
					coverUrl=isHasImg(coverUrl);
					var thislist =
						'<li>' +
						'<a href="4.1.0qiugou_detail.html?id=' + n.id + '">' +
						'<img class="qiugouclass" src="' + coverUrl + '" width="230" height="250" />' +
						'</a>' +
						'</li>';
					$('#QiuGouGoodsList').append(thislist);
				});
			}
			
			//固定宽高
			$(function(){
				var imgList1 = document.getElementsByClassName("qiugouclass");  
			    var w=230;
			    var h=250;
			    for(var i=0;i<imgList1.length;i++){
			        if(imgList1[i].width>w || imgList1[i].height>h){  
			            imgList1[i].width = w;  
			            imgList1[i].heigth = h;  
			        }  
			    }  
			})
		},
		error: function() {
			layer.alert("error");
		}
	});
}

function selectGoodsBySortType(sortTypeId) {
	selectGoodsByQiugou(sortTypeId);
}

function selectGoodsByWeiHuo(styleSort) {
	$("#WeiHuoList").html("");
	$.ajax({
		url: urlcore + "/api/poop/selectPoopList?propertyTypeId=" + styleSort + "&size=" + 8 + "&current=" + 1,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				$.each(data.data.list, function(i, n) {
					
					var coverUrl=n.goods.coverUrl==null?'http://app.jiagangwangluo.com:8080/api//ATTACHMENT/cab06117-6607-401a-843d-5ac103323fb6/default.png':n.goods.coverUrl;
					coverUrl=isHasImg(coverUrl);
					var goodsTitle=n.goods.goodsTitle==null?'暂无':n.goods.goodsTitle;
					var tradePrice=n.goods.tradePrice==null?'0':n.goods.tradePrice;
					var packPrice=n.goods.packPrice==null?'0':n.goods.packPrice;
					var collectNum=n.goods.collectNum==null?'0':n.goods.collectNum;
					var thislist =
						'<dl>' +
						'<a href="2.2.0goods_detail.html?id=' + n.goods.id + '">' +
						'<dt>' +
						'<img class="weihuoclass" src="' + coverUrl + '"  style="width: 229px;height: 290px;"/>' +
						'</dt>' +
						'<dd>' +
						'<h5>' + goodsTitle + '</h5>' +
						'<h6>&yen;' + tradePrice + '-' + packPrice + '<span>收藏 ' + collectNum + '</span>' + '</h6>' +
						'</dd>' +
						'</a>' +
						'</dl>';
					$('#WeiHuoList').append(thislist);
				});
			}
			
			//固定宽高
			$(function(){
				var imgList1 = document.getElementsByClassName("weihuoclass");  
			    var w=290;
			    var h=290;
			    for(var i=0;i<imgList1.length;i++){
			        if(imgList1[i].width>w || imgList1[i].height>h){  
			            imgList1[i].width = w;  
			            imgList1[i].heigth = h;  
			        }  
			    }  
			})
		},
		error: function() {
			layer.alert("error");
		}
	});
}

function selectWeiHuo(styleSort) {
	selectGoodsByWeiHuo(styleSort);
}

function selectAreaList() {
	$("#areaListToIndex").html("");
	$.ajax({
		url: urlcore + "/api/tradeArea/selectTradeShopGoodsList?size=" + 8 + "&current=" + 1,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				$.each(data.data.list, function(i, n) {
					
					var areaUrl=n.areaUrl==null?'http://app.jiagangwangluo.com:8080/api//ATTACHMENT/cab06117-6607-401a-843d-5ac103323fb6/default.png':n.areaUrl;
					areaUrl=isHasImg(areaUrl);
					var areaName=n.areaName==null?'暂无':n.areaName;
					var characteristic=n.characteristic==null?'0':n.characteristic;
					var price=n.price==null?'0':n.price;
					var thislist =
						'<dl>' +
						'<a href="8.0.0shangquan.html?id=' + n.id + '">' +
						'<dt>' +
						'<img class="shangquanclass" src="' + areaUrl + '" />' +
						'</dt>' +
						'<dd>' +
						'<h5>' + areaName + '</h5>' +
						'<p>' + characteristic + '</p>' +
						'<h6>均价: <span>&yen;' + price + '</span></h6>' +
						'</dd>' +
						'</a>' +
						'</dl>';
					$('#areaListToIndex').append(thislist);
				});
			}
			
			//固定宽高
			$(function(){
				var imgList1 = document.getElementsByClassName("shangquanclass");  
			    var w=290;
			    var h=280;
			    for(var i=0;i<imgList1.length;i++){
			        if(imgList1[i].width>w || imgList1[i].height>h){  
			            imgList1[i].width = w;  
			            imgList1[i].heigth = h;  
			        }  
			    }  
			})
			
		},
		error: function() {
			layer.alert("error");
		}
	});
}

function selectShopList(sortTypeId) {
	$("#shopList").html("");
	$.ajax({
		url: urlcore + "/api/tradeArea/selectTradeGoodsList?sortTypeId=" + sortTypeId + "&sort=" + 6 + "&size=" + 8 + "&current=" + 1,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				$.each(data.data.list, function(i, n) {
					var headImg = n.headImg==null?'http://app.jiagangwangluo.com/api//ATTACHMENT/cab06117-6607-401a-843d-5ac103323fb6/sqz.png':n.headImg;
				    headImg=isHasImg(headImg);
				    var storeName = n.storeName==null?'暂无':n.storeName;
                    var province = n.province==null?'暂无':n.province;
                    var city = n.city==null?'暂无':n.city;
                    var areaName = n.tradeArea.areaName==null?'暂无':n.tradeArea.areaName;
                    var monthNew = n.monthNew==null?'0':n.monthNew;
                    var saleMoney = n.saleMoney==null?'0':n.saleMoney;
					var thislist =
						'<dl style="width:290px;">' +
						'<a href="10.1.0dianpu_detail.html?id=' + n.id + '">' +
						'<dt style="width:290px;">' +
						'<img class="shopClass" src="' + headImg + '" style="width:290px;"/>' +
						'</dt>' +
						'<dd>' +
						'<h5>' +
						'<img src="images/dianpu.png" />&nbsp;' +
						storeName +
						'</h5>' +
						'<p>' + province + '-' + city + '-' + areaName + '</p>' +
						'<div class="clearfix index_dianpu_xin">' +
						'<span class="fl">上新 ：<em>' + monthNew + '</em></span>' +
						'<span class="fr">销售额：<em>' + saleMoney + '</em></span>' +
						'</div>' +
						'</dd>' +
						'</a>' +
						'</dl>';
					$('#shopList').append(thislist);
				});
			}
			
			//固定宽高
			$(function(){
				var imgList1 = document.getElementsByClassName("shopClass");  
			    var w=290;
			    var h=280;
			    for(var i=0;i<imgList1.length;i++){
			        if(imgList1[i].width>w || imgList1[i].height>h){  
			            imgList1[i].width = w;  
			            imgList1[i].heigth = h;  
			        }  
			    }  
			})
		},
		error: function() {
			layer.alert("error");
		}
	});
}

function selectShopBySortType(sortTypeId) {
	selectShopList(sortTypeId);
}

function selectGoodsByGoldRate(styleSort) {
	$("#goldList").html("");
	$.ajax({
		url: urlcore + "/api/goods/selectGoodsList?propertyTypeId=" + styleSort + "&goldRate=" + 1 + "&size=" + 8 + "&current=" + 1,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				$.each(data.data.list, function(i, n) {
					
					var coverUrl=n.coverUrl==null?'http://app.jiagangwangluo.com:8080/api//ATTACHMENT/cab06117-6607-401a-843d-5ac103323fb6/default.png':n.coverUrl;
					coverUrl=isHasImg(coverUrl);
					var goodsTitle=n.goodsTitle==null?'暂无':n.goodsTitle;
					var tradePrice=n.tradePrice==null?'0':n.tradePrice;
					var goldRate=n.goldRate==null?'0':n.goldRate;
					var collectNum=n.collectNum==null?'0':n.collectNum;
					var thislist =
						'<dl >' +
						'<a href="2.2.0goods_detail.html?id=' + n.id + '">' +
						'<dt style="width: 230px;">' +
						'<img class="jinbiClass" src="' + coverUrl + '" style="width: 230px;"/>' +
						'</dt>' +
						'<dd>' +
						'<h5>' + goodsTitle + '</h5>' +
						'<h6>&yen;' + tradePrice + '<em>&nbsp;金币可抵' + goldRate + '%</em><span>收藏' + collectNum + '</span></h6>' +
						'</dd>' +
						'</a>' +
						'</dl>';
					$('#goldList').append(thislist);
				});
				
				//固定宽高
				$(function(){
					var imgList1 = document.getElementsByClassName("jinbiClass");  
				    var w=290;
				    var h=290;
				    for(var i=0;i<imgList1.length;i++){
				        if(imgList1[i].width>w || imgList1[i].height>h){  
				            imgList1[i].width = w;  
				            imgList1[i].heigth = h;  
				        }  
				    }  
				})
			}
		},
		error: function() {
			layer.alert("error");
		}
	});
}

function selectGoldRate(styleSort) {
	selectGoodsByGoldRate(styleSort);
}

function selectGoodsByGoodsType(goodsType, lookTime, priceLevel) {
	$(document).ready(function() {
		init(currentPage);
	});

	function init(pageNo) {
		$("#jGGoodsList").html("");
		$.ajax({
			url: urlcore + "/api/goods/selectGoodsList?goodsType=" + goodsType + "&sort=" + lookTime + "&priceLevel=" + priceLevel + "&size=" + 16 + "&current=" + pageNo,
			type: "get",
			dataType: 'json',
			async: false,
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					$.each(data.data.list, function(i, n) {
						
						var coverUrl=n.coverUrl==null?'http://app.jiagangwangluo.com:8080/api//ATTACHMENT/cab06117-6607-401a-843d-5ac103323fb6/default.png':n.coverUrl;
						coverUrl=isHasImg(coverUrl);
						var goodsTitle=n.goodsTitle==null?'暂无':n.goodsTitle;
						var tradePrice=n.tradePrice==null?'0':n.tradePrice;
						var packPrice=n.packPrice==null?'0':n.packPrice;
						var goldRate=n.goldRate==null?'0':n.goldRate;
						var collectNum=n.collectNum==null?'0':n.collectNum;
						
						var thislist =
							'<dl>' +
							'<a href="2.2.0goods_detail.html?id=' + n.id + '">' +
							'<dt>' +
							'<img class="searchMyGoods" src="' + coverUrl + '" />' +
							'</dt>' +
							'<dd>' +
							'<h5>' + goodsTitle + '</h5>' +
							'<h6>&yen;' + tradePrice + '-' + packPrice + '<span>收藏' + collectNum + '</span></h6>' +
							'<em>物质社会</em>' +
							'</dd>' +
							'</a>' +
							'</dl>';
						$('#jGGoodsList').append(thislist);
					});
					
					//固定宽高
					$(function(){
						var imgList1 = document.getElementsByClassName("searchMyGoods");  
					    var w=290;
					    var h=290;
					    for(var i=0;i<imgList1.length;i++){
					        if(imgList1[i].width>w || imgList1[i].height>h){  
					            imgList1[i].width = w;  
					            imgList1[i].heigth = h;  
					        }  
					    }  
					})
					

					$("#pager").pager({
						pagenumber: pageNo,
						pagecount: data.data.pages,
						totalcount: data.data.total,
						buttonClickCallback: PageClick
					});

				} else if(data.code == 'OVERTIME') {
					var thisUrl = window.location.href;
					//alert(thisUrl);
					if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
						top.window.location.href = "1.0.0denglu.html";
					}

				} else {
					if(data.msg != '空数据') {
						layer.alert(data.msg)
					} else {
						$('#thiscount').text(0);
					}
				}

			},
			error: function() {
				layer.alert("error");
			}
		});
	}
	PageClick = function(pageclickednumber) {
		init(pageclickednumber);
	}
}

function selectGoodsByType(goodsType, lookTime, priceLevel) {
	selectGoodsByGoodsType(goodsType, lookTime, priceLevel);
}

function advertisementList() {
	$("#guanggaoList").html("");
	$.ajax({
		url: urlcore + "/api/eachPicture/selectEachPictureListByType?type="+12+"&carouselType="+1,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				$.each(data.data, function(i, n) {
					/*var thislist =
						'<li style="background-image: url(' + n.imgUrl + ');">' +
						'<a href="#"></a>' +
						'</li>';
					$('#guanggaoList').append(thislist);*/
						if(n.auctionType != null) {
							
							var imgUrl=isHasImg1(n.imgUrl);
							if(n.auctionType == 1) {
								var thislist =
									'<li style="background-image: url(' + imgUrl + ');">' +
									'<a class="carousel1" href="10.1.0dianpu_detail.html?id=' + n.auctionId + '&type=1"></a>' +
									'</li>';
							} else {
								var thislist =
									'<li style="background-image: url(' + imgUrl + ');">' +
									'<a class="carousel1" href="2.2.0goods_detail.html?id=' + n.auctionId + '&type=1"></a>' +
									'</li>';
							}
						$('#guanggaoList').append(thislist);
						
					}

					
				});
				$(function() {
						jQuery(".fullSlide").slide({
							titCell: ".hd ul",
							mainCell: ".bd ul",
							effect: "fold",
							autoPlay: true,
							autoPage: true,
							trigger: "click",
							startFun: function(i) {
								//alert(i);
								var curLi = jQuery(".fullSlide .bd li").eq(i); /* 当前大图的li */
								//alert(curLi.length);
								if(!!curLi.attr("_src")) {
									curLi.css("background-image", curLi.attr("_src")).removeAttr("_src") /* 将_src地址赋予li背景，然后删除_src */
								}
							}
						});
					})

			} else {
				layer.alert(data.msg);
			}
		},
		error: function() {
			layer.alert("error");
		}
	});
}

function sortTypeMethod() {
	$("#sortTypeLists").html("");
	$.ajax({
		url: urlcore + "/api/sortType/allSortTypeListPage?size=" + 8 + "&current=" + 1,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {

			if(data.success == true) {
				$.each(data.data.list, function(i, n) {
					var thislist =
						'<li>' +
						'<a href="javascript:selectGoodsBySortType(' + n.id + ');">' + n.sortName + '</a>' +
						'</li>';
					$('#sortTypeLists').append(thislist);
				});
			}
		},
		error: function() {
			layer.alert("error");
		}
	});
}

function sortTypeMethod1() {
	$("#shopTypeList").html("");
	$.ajax({
		url: urlcore + "/api/sortType/allSortTypeListPage?size=" + 8 + "&current=" + 1,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {

			if(data.success == true) {
				$.each(data.data.list, function(i, n) {
					var thislist =
						'<li>' +
						'<a href="javascript:selectShopBySortType(' + n.id + ');">' + n.sortName + '</a>' +
						'</li>';
					$('#shopTypeList').append(thislist);
				});
			}
		},
		error: function() {
			layer.alert("error");
		}
	});
}

function getUserInfoByUserId() {
	$("#userInfo").html("");
	$.ajax({
		url: urlcore + "/api/user/getUserInfo",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				var da = data.data;
				var headImg = n.headImg==null?'http://app.jiagangwangluo.com/api//ATTACHMENT/cab06117-6607-401a-843d-5ac103323fb6/sqz.png':n.headImg;
				var userName = n.headImg==null?'暂无':da.userName;
				var thislist =
					'<dl class="fl clearfix">' +
					'<dt>' +
					'<img src="' + headImg + '">' +
					'</dt>' +
					'<dd class="putong_geren_dd">' +
					'<h6>' + userName + '</h6>' +
					'<p>会员等级：<span>LV2</span></p>' +
					'<p>注册时间：' + new Date(da.gmtDatetime).pattern("yyyy-MM-dd") + '</p>' +
					'</dd>' +
					'</dl>';
				$('#userInfo').append(thislist);
			}
		},
		error: function() {
			layer.alert("error");
		}
	});
}
 
